#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;

void printArr(vector<int> &array)
{
    for (auto a : array)
        std::cout << a << " ";
    std::cout << std::endl;
}

struct Goods
{
    string _name;  // 名字
    double _price; // 价格
    int _evaluate; // 评价
    Goods(const char *str, double price, int evaluate)
        : _name(str), _price(price), _evaluate(evaluate)
    {
    }
};

void test1()
{
    vector<int> array = {4, 1, 8, 5, 3, 7, 0, 9, 2, 6};

    printArr(array);
    // 默认按照小于比较，排出来结果是升序
    std::sort(array.begin(), array.end());
    printArr(array);

    // 如果需要降序，需要改变元素的比较规则
    std::sort(array.begin(), array.end(), std::greater<int>());
    printArr(array);
}

void test2()
{
    vector<Goods> v = {{"苹果", 2.1, 5}, {"香蕉", 3, 4}, {"橙子", 2.2, 3}, {"菠萝", 1.5, 4}};
    sort(v.begin(), v.end(), [](const Goods &g1, const Goods &g2)
         { return g1._price < g2._price; });
    sort(v.begin(), v.end(), [](const Goods &g1, const Goods &g2)
         { return g1._price > g2._price; });
    sort(v.begin(), v.end(), [](const Goods &g1, const Goods &g2)
         { return g1._evaluate < g2._evaluate; });
    sort(v.begin(), v.end(), [](const Goods &g1, const Goods &g2)
         { return g1._evaluate > g2._evaluate; });
}

void test3()
{
    auto e = [] {};
    std::cout << typeid(e).name() << std::endl; // Z5test3vEUlvE_

    int a = 3, b = 4;
    auto func1 = [=]
    { return a + 3; };
    int ret = func1();
    std::cout << ret << std::endl;

    auto fun1 = [&](int c)
    { b = a + c; };
    fun1(10);
    cout << a << " " << b << endl;

    auto fun2 = [=, &b](int c) -> int
    { return b += a + c; };
    cout << fun2(10) << endl;
    cout << b << endl;

    int x = 10;
    auto add_x = [x](int a) mutable
    {
        x *= 2;
        return a + x;
    };
    cout << add_x(10) << endl;
    cout << x << endl;
}

int main()
{
    test3();
    return 0;
}